<template>
  <div class="favorite-page">
    <div class="header">
      <h2>我的收藏</h2>
    </div>

    <div v-if="loading" class="state loading">加载中...</div>
    <div v-else-if="error" class="state error">{{ error }}</div>
    <div v-else>
      <div v-if="favorites.length === 0" class="state empty">
        暂无收藏的房源
      </div>
      <div v-else class="list">
        <div v-for="item in favorites" :key="item.id" class="card">
          <img class="cover" :src="item.housePicture" alt="cover" />
          <div class="content">
            <div class="title">{{ item.houseTitle }}</div>
            <div class="meta">
              <span class="address">{{ item.address }}</span>
              <span class="dot">·</span>
              <span class="area">{{ item.rentArea }}</span>
            </div>
            <div class="price">
              <span class="value">{{ item.rentPriceListing }}</span>
              <span class="unit">/{{ item.rentPriceUnit }}</span>
            </div>
          </div>
          <button class="unfav" :disabled="removingId === item.id" @click="onUnfavorite(item)">
            {{ removingId === item.id ? '处理中...' : '取消收藏' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { getFavorites, removeFavorite } from '@/api/favorite.js'

const favorites = ref([])
const loading = ref(false)
const error = ref('')
const removingId = ref(null)

async function fetchFavorites() {
  loading.value = true
  error.value = ''
  try {
    const list = await getFavorites()
    favorites.value = list || []
  } catch (e) {
    error.value = '获取收藏列表失败'
    console.error(e)
  } finally {
    loading.value = false
  }
}

async function onUnfavorite(item) {
  if (!item?.id) return
  removingId.value = item.id
  try {
    await removeFavorite(item.id)
    favorites.value = favorites.value.filter((f) => f.id !== item.id)
  } catch (e) {
    console.error('取消收藏失败', e)
    alert('取消收藏失败，请稍后重试')
  } finally {
    removingId.value = null
  }
}

onMounted(fetchFavorites)
</script>

<style scoped>
.favorite-page {
  display: flex;
  flex-direction: column;
  padding: 16px;
}

.header {
  margin-bottom: 12px;
}

.header h2 {
  margin: 0;
  font-size: 18px;
}

.state {
  padding: 24px 0;
  text-align: center;
  color: #666;
}

.state.error {
  color: #d33;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  display: grid;
  grid-template-columns: 112px 1fr auto;
  gap: 12px;
  align-items: center;
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.cover {
  width: 112px;
  height: 84px;
  object-fit: cover;
  border-radius: 6px;
  background: #f5f5f5;
}

.content {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.title {
  font-size: 14px;
  font-weight: 600;
  color: #222;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta {
  font-size: 12px;
  color: #666;
}

.meta .dot { padding: 0 6px; }

.price { color: #ff6a00; font-weight: 700; }
.price .value { font-size: 16px; }
.price .unit { font-size: 12px; margin-left: 2px; }

.unfav {
  border: 1px solid #e5e5e5;
  background: #fff;
  color: #333;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.unfav:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>